package com.cloud.pole.weixin.wechat;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloud.pole.common.constant.CacheConstant;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.weixin.model.entity.WxApp;
import com.cloud.pole.weixin.util.wechat.CommonRequest;
import com.cloud.pole.weixin.wechat.domain.AccessToken;
import com.cloud.pole.weixin.wechat.http.HttpConstant;
import lombok.extern.slf4j.Slf4j;

/*
*2019年5月12日
*cloud-weixin
*author:frank-chen
*/
@Slf4j
public class TestRequest {


	public static AccessToken getAccessToken(String appId, String appSecret) throws Exception {
		//获取AccessToken
		String strUrl = HttpConstant.getAccessTokenURL(appId, appSecret);
		String strResult = CommonRequest.doGet(strUrl);
		JSONObject accessJson = JSONObject.parseObject(strResult);
		if(accessJson.containsKey("errcode")){
			String errcode = accessJson.getString("errcode");
			String errmsg = accessJson.getString("errmsg");
			log.error("appId[" + appId + "] errcode[" + errcode + "] errmsg[" + errmsg + "]");
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, errcode + ":" + errmsg);
		}
		AccessToken accessToken = JSONObject.parseObject(strResult, AccessToken.class);
		accessToken.setCreateTime(new Date());
		return accessToken;
	}

	public static void main(String[] args) {
		AccessToken accessToken;
		try {
			accessToken = TestRequest.getAccessToken("wxd998b8746beb6657", "82ded8d849b93be746ba65f0de1defa0");
			System.out.println(accessToken.getAccess_token());

			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
